package com.knowledge.plus.深拷贝和浅拷贝;

import com.fasterxml.jackson.databind.ObjectMapper;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;

/**
 * @author TPF
 * @since 2024/11/18
 */
public class JacksonDemo {

    private static final ObjectMapper objectMapper = new ObjectMapper();

     public static <T> T deepClone(T obj) throws Exception {
        String json = objectMapper.writeValueAsString(obj);
        return objectMapper.readValue(json, (Class<T>) obj.getClass());
    }

     public static void main(String[] args) throws Exception {
        Address address = new Address("浙江省", "杭州市");
        Person person1 = new Person("张珊", address);

        Person person2 = deepClone(person1);
        System.out.println(person1.getAddress() == person2.getAddress());  // 结果：false
    }

}

@Data
@NoArgsConstructor
@AllArgsConstructor
class JsonPerson {
    private String name;
    private Address address;
}

@Data
@NoArgsConstructor
@AllArgsConstructor
class JsonAddress {
    private String province;
    private String city;
}